﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using viBook.Common;
using viBook.Models;
using viBook.Helpers;
using viBook.Repositories;

namespace viBook.Controllers
{
    public class AuthorsController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IAuthorRepository _authorRepository;
        private readonly IContentTagRepository _contenttagRepository;
        private readonly IUserRepository _userRepository;
        private readonly IBookRepository _bookRepository;
        public AuthorsController(
                                IAuthorRepository authorRepository,
                                IContentTagRepository contenttagRepository,
                                IUserRepository userRepository,
                                IBookRepository bookRepository)
        {
            _authorRepository = authorRepository;
            _contenttagRepository = contenttagRepository;
            _userRepository = userRepository;
            _bookRepository = bookRepository;
        }

        #endregion

        // TODO: Move to config file
        [OutputCache(Duration = 5, VaryByParam = "q")]
        public string Search(string q)
        {
            var result = _authorRepository.FindAuthors(q, Constant.TakedItem);
            return String.Join(Environment.NewLine, result);
        }

        public ActionResult Details(int id)
        {
            ViewBag.ListMostPopularTags = _contenttagRepository.GetMostPopularTags();
            ViewBag.ListMostPopularUser = _userRepository.GetMostPopularUsers();
            ViewBag.ListMostPopularBook = _bookRepository.GetMostPopularBook();
            var author = _authorRepository.FindAuthorWithAllBooks(id);
            return View(author);
        }

        public JsonResult FetchNextBookPage(int authorId, int pageNumber)
        {
            var listBooks = _authorRepository.FetchNextBookPage(authorId, pageNumber).ToList();
            return Json(listBooks.ListBookToJsonResult());
        }

        
    }
}